package com.loren.demo20.service;

import com.loren.demo20.entity.User;
import com.loren.demo20.form.UserForm;
import com.loren.demo20.repository.UserRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserService {

    @Resource
    private DemoService demoService;

    @Resource
    private UserRepository userRepository;

    public User getUserInfo(long id) {
        System.out.println("...call UserService.getUserInfo...");
        this.demoService.demo();
        return this.userRepository.findOne(id);
    }

    public void saveUserInfo(UserForm form) {
        System.out.println("...call UserService.saveUserInfo...");
        User user = new User(form.getUsername(), form.getPassword());
        this.userRepository.save(user);
    }

    public void updateUserInfo(long userId, UserForm form) {
        System.out.println("...call UserService.updateUserInfo...");
        this.userRepository.update(new User(userId, form.getUsername(), form.getPassword()));
        this.doSomething(userId);
    }

    public void callSaveOrUpdate() {
        long userId = this.userRepository.save(new User(2L, "haha", "123456"));
        this.userRepository.update(new User(userId, "haha", "1234567"));
    }

    private void doSomething(long userId) {
        System.out.println("...call UserService.doSomething...");
        User user = this.userRepository.findOne(userId);
        System.out.println(user);
    }
}
